﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HREDU.Hoang_Tuan.Classes;
using HREDU.Hoang_Tuan;

namespace HREDU
{
    
    public partial class frmChonMenu : Form
    {
        public string[] strNames = new string[4]{"Chọn", "Id","Name","Key"};
        frmMain frmCall; 
        DataTable bangMenu = new DataTable(); 
        public frmChonMenu(frmMain frm)
        {
            InitializeComponent();

            frmCall = frm;
        }

        private void frmChonMenu_Load(object sender, EventArgs e)
        {
            //tao bang
            bangMenu.Columns.Add(strNames[0], System.Type.GetType("System.Boolean"));
            bangMenu.Columns.Add(strNames[1], System.Type.GetType("System.String"));
            bangMenu.Columns.Add(strNames[2], System.Type.GetType("System.String"));
            bangMenu.Columns.Add(strNames[3], System.Type.GetType("System.String"));
            int menu_id = 0;
            int count = 0;
            foreach (ToolStripMenuItem menu in frmCall.menuStrip1.Items)
            {
                menu_id += 1;
                count = 0;
                for (int i = 0; i < menu.DropDownItems.Count; i++)
                {
                    //Lenh Try Catch nay dung de chan menu.DropDownItems(i) la ToolStripSeparator
                    try
                    {
                        ToolStripMenuItem m = (ToolStripMenuItem)System.Convert.ChangeType(menu.DropDownItems[i], typeof(ToolStripMenuItem));
                        count += 1;
                        DataRow dong1 = bangMenu.NewRow();
                        dong1[0] = false;
                        if (count < 10)
                            dong1[1] = menu_id.ToString() + "0" + count.ToString();
                        else
                            dong1[1] = menu_id.ToString() + count.ToString();
                        dong1[2] = m.Text;
                        dong1[3] = m.Name;
                        bangMenu.Rows.Add(dong1);
                        gridDMMenu.DataSource = bangMenu;
                        gridDMMenu.Columns[strNames[1]].ReadOnly = true;
                        gridDMMenu.Columns[strNames[2]].ReadOnly = true;
                        gridDMMenu.Columns[strNames[3]].ReadOnly = true;
                    }
                    catch
                    {
                    }
                }
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            //lay tat ca cac menu khong duoc check va luu vao co so du lieu
            string sInsert = "delete from Menu";
            foreach (DataGridViewRow dong in gridDMMenu.Rows)
            {
                if ((bool)dong.Cells[strNames[0]].Value)
                    sInsert += " insert into Menu values(" + dong.Cells[strNames[1]].Value.ToString().Trim() + ",N'" + dong.Cells[strNames[2]].Value.ToString().Trim() + "',N'" + dong.Cells[strNames[3]].Value.ToString().Trim() + "')";
            }

            if (MITI.BaseDB.Instance.ExecSql_NoneQuery(sInsert) > 0)
                MessageBox.Show("Thành công");
            else
                MessageBox.Show("Thất bại");
        }

        private void chkCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dr in gridDMMenu.Rows)
            {
                if (dr.Cells[strNames[2]].Value != "Đăng nhập"
                && dr.Cells[strNames[2]].Value != "Đăng xuất"
                && dr.Cells[strNames[2]].Value != "&Thoát"
                    && dr.Cells[strNames[2]].Value != "Cascade"
                    && dr.Cells[strNames[2]].Value != "Arrange"
                    && dr.Cells[strNames[2]].Value != "Title Vertical"
                    && dr.Cells[strNames[2]].Value != "Title Horizon"
                    )
                dr.Cells[strNames[0]].Value = chkCheckAll.Checked;
            
            }
        
        }
    }
}